package com.liujit.standard.basis.core.util;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.SecureRandom;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author : liu.q [916000612@qq.com]
 * @少时狂发编程想,无谓赴身IT行.
 * @纵使荣华未可进,我自coding又何妨!
 * @Description :
 */

public class VerifyImgUtil {
    /*源文件宽度*/
    private static int T_WIDTH = 320;
    /*源文件高度*/
    private static int T_HEIGHT = 180;


    /**
     * @Love : 写这段代码的时候，只有上帝和我知道它是干嘛的.
     * @conversation : 现在，只有上帝知道
     * @Author : liu.q [916000612@qq.com]
     * @Date : 2019-12-07 23:08
     * @Description : 随机生成抠图坐标
     */
    public static Map<String, Integer> generateCutoutCoordinates(int w, int h) {
        Map<String, Integer> result = new HashMap();
        int x = new SecureRandom().nextInt(T_WIDTH - w);
        int y = new SecureRandom().nextInt(T_HEIGHT - h);
        while (x < w + 10) {
            x = new SecureRandom().nextInt(275);
        }
        result.put("x", x);
        result.put("y", y);
        return result;
    }

    /**
     * @Love : 写这段代码的时候，只有上帝和我知道它是干嘛的.
     * @conversation : 现在，只有上帝知道
     * @Author : liu.q [916000612@qq.com]
     * @Date : 2019-12-07 23:08
     * @Description : 生成滑块
     */
    public static Map<String, BufferedImage> initSlidingBlock(BufferedImage sourceImg, int[][] templateImgData, int x, int y, int w, int h) throws Exception {
        // 支持alpha通道的rgb图像
        BufferedImage newTemplateImg = new BufferedImage(w, h, BufferedImage.TYPE_4BYTE_ABGR);

        for (int i = 0; i < templateImgData.length; i++) {
            for (int j = 0; j < templateImgData[0].length; j++) {
                int rgb = templateImgData[i][j];
                if (rgb == 1) {
                    int rgb_ori = sourceImg.getRGB(x + i, y + j);
                    int r = (0xff & rgb_ori);
                    int g = (0xff & (rgb_ori >> 8));
                    int b = (0xff & (rgb_ori >> 16));
                    rgb_ori = r + (g << 8) + (b << 16) + (255 << 24);
                    newTemplateImg.setRGB(i, j, rgb_ori);

                    //创建遮罩层
                    BufferedImage cover = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
                    Graphics2D coverG2 = (Graphics2D) cover.getGraphics();
                    coverG2.setColor(Color.BLACK);
                    coverG2.fillRect(0, 0, 10, 10);
                    coverG2.dispose();
                    Graphics2D bgG2 = (Graphics2D) sourceImg.getGraphics();
                    //开启透明度
                    bgG2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.5f));
                    //描绘
                    bgG2.drawImage(cover, i + x, y + j, 1, 1, null);
                    //结束透明度
                    bgG2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
                    bgG2.dispose();
                }
                if (rgb == 0) {
                    //不用搭理
                }
                if (rgb == 2) {
                    //秒边 16777215
                    newTemplateImg.setRGB(i, j, 8421504);
                    sourceImg.setRGB(x + i, y + j, 16777215);
                }
            }
        }
        Map<String, BufferedImage> result = new HashMap<>();
        result.put("newTemplateImg", newTemplateImg);
        result.put("newSourceImg", sourceImg);
        return result;
    }


    /**
     * @Love : 写这段代码的时候，只有上帝和我知道它是干嘛的.
     * @conversation : 现在，只有上帝知道
     * @Author : liu.q [916000612@qq.com]
     * @Date : 2019-12-07 23:08
     * @Description : 获取base64
     */
    public static String getBase64(BufferedImage bi) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        String res = "";
        try {
            ImageIO.write(bi, "png", outputStream);
            res = Base64.getEncoder().encodeToString(outputStream.toByteArray());
            res = "data:image/png;base64," + res;
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return res;
    }

    /**
     * @Love : 写这段代码的时候，只有上帝和我知道它是干嘛的.
     * @conversation : 现在，只有上帝知道
     * @Author : liu.q [916000612@qq.com]
     * @Date : 2019-12-07 23:08
     * @Description : 生成抠图图形矩阵
     */
    public static int[][] getTemplateData(BufferedImage bi) {
        int[][] data = new int[bi.getWidth()][bi.getHeight()];
        for (int i = 0; i < bi.getWidth(); i++) {
            for (int j = 0; j < bi.getHeight(); j++) {
                int rgb = bi.getRGB(i, j);
                int leftRgb = -1, rightRgb = -1, upRgb = -1, downRgb = -1;
                if (j > 0) {
                    leftRgb = bi.getRGB(i, j - 1);
                }
                if (j < bi.getHeight() - 1) {
                    rightRgb = bi.getRGB(i, j + 1);
                }
                if (i > 0) {
                    upRgb = bi.getRGB(i - 1, j);
                }
                if (i < bi.getWidth() - 1) {
                    downRgb = bi.getRGB(i + 1, j);
                }
                if (rgb == -1 && (i == 0 || i == bi.getWidth() - 1 || j == 0 || j == bi.getHeight() - 1)) {
                    //边
                    data[i][j] = 2;
                } else if (rgb == -1 && (leftRgb != -1 || rightRgb != -1 || upRgb != -1 || downRgb != -1)) {
                    //边
                    data[i][j] = 2;
                } else if (rgb == -1) {
                    //内容区域
                    data[i][j] = 1;
                } else {
                    //透明区域
                    data[i][j] = 0;
                }
            }
        }
        return data;
    }
}
